import logging
module_logger = logging.getLogger('SP.controllers.server')

def index():
    return ""

@auth.requires_login()
@service.xmlrpc
def get_version():
    """Returns the version of the braintrainer XMLRPC protocol used.
    This is here to make sure that a next version protocol can be easily implemented without breaking stuff"""
    version = settings.XMLRPCVersion
    return dict(result=version)
    
@auth.requires_login()
@service.xmlrpc 
def get_session_id():
    """Should be called right after creating the proxy in the client,
    because each client MUST have an unique session_id.
    Returns unique UUID"""
    session_id = _get_session_id()
    return dict(result=session_id)

@auth.requires_login()
@service.xmlrpc
def open_and_return_session():
    """Adds client info to the table 'db.session' to create a session for the client.
    Returns db.session.id"""
    session_id = _open_and_return_session()
    return dict(result=session_id)

@auth.requires_login()
@service.xmlrpc
def close_session(session_id):
    """Registers the end_time of the session and closes the session."""
    _close_session(session_id)

@auth.requires_login()
@service.xmlrpc
def get_levelupcount(session_id, game_name):
    """Gets the amount of turns which ends a level
    Return Integer
    Parameters: String session_id - unique client session ID
                String game_name - game_name of the current client"""
    levelupcount = _get_levelupcount(session_id, game_name)
    return dict(result=levelupcount)

@auth.requires_login()
@service.xmlrpc
def get_levels(session_id, game_name):
    """Gets amount of levels
    Return Integer
    Parameters: String session_id - unique client session ID
                String game_name - game_name of the current client"""
    levels = _get_levels(session_id, game_name)
    return dict(result=levels)

@auth.requires_login()
@service.xmlrpc
def get_user(session_id):
    """Gets name of user
    Return String
    Parameters: String session_id - unique client session ID"""
    user = _get_user(session_id)
    return dict(result=user)

@auth.requires_login()
@service.xmlrpc
def get_difficulty(session_id):
    """Gets the current level the client should be running
    Return Integer
    Parameters: String session_id - unique client session ID"""
    difficulty = _get_difficulty(session_id)
    return dict(result=difficulty)

@auth.requires_login()
@service.xmlrpc
def set_difficulty(session_id, level):
    _set_difficulty(session_id, level)

@auth.requires_login()
@service.xmlrpc
def set_level_end(session_id, game_data):
    #data = {'store_db':False, 'levelup':False, 'score':0}
    """Gets the current level the client should be running
    Parameters: String session_id - unique client session ID
                Dictionary game_data - dictionary with game_data to decide whether to level up or down
    Format: game_data - {'level':Integer, 'store_db':Boolean, 'levelup':Boolean, 'score':Integer}"""
    _set_level_end(session_id, game_data)

@auth.requires_login()
@service.xmlrpc
def set_start_time(session_id):
    """Sets start_time for current played level.
    Parameters: String session_id - unique client session ID"""
    _set_start_time(session_id)

@auth.requires_login()
@service.xmlrpc
def set_end_time(session_id):
    """Sets end_time for current played level.
    Parameters: String session_id - unique client session ID"""
    _set_end_time(session_id)
    
@auth.requires_login()
@service.xmlrpc
def calculate_time(session_id):
    timespend = _calculate_time(session_id)
    return dict(result=timespend)

## DEFAULT WEB2PY CONTROLLER STUFF

def user():
    """
    exposes:
    http://..../[app]/default/user/login
    http://..../[app]/default/user/logout
    http://..../[app]/default/user/register
    http://..../[app]/default/user/profile
    http://..../[app]/default/user/retrieve_password
    http://..../[app]/default/user/change_password
    use @auth.requires_login()
        @auth.requires_membership('group name')
        @auth.requires_permission('read','table name',record_id)
    to decorate functions that need access control
    """
    return dict(form=auth())
    
#@auth.requires_login()
def download():
    """
    allows downloading of uploaded files
    http://..../[app]/default/download/[filename]
    """
    return response.download(request,db)

@auth.requires_login()
def call():
    """
    exposes services. for example:
    http://..../[app]/default/call/jsonrpc
    decorate with @services.jsonrpc the functions to expose
    supports xml, json, xmlrpc, jsonrpc, amfrpc, rss, csv
    """
    
    return service()

@auth.requires_signature()
def data():
    """
    http://..../[app]/default/data/tables
    http://..../[app]/default/data/create/[table]
    http://..../[app]/default/data/read/[table]/[id]
    http://..../[app]/default/data/update/[table]/[id]
    http://..../[app]/default/data/delete/[table]/[id[
    http://..../[app]/default/data/select/[table]
    http://..../[app]/default/data/search/[table]
    but URLs bust be signed, i.e. linked with
      A('table',_href=URL('data/tables',user_signature=True))
    or with the signed load operator
      LOAD('default','data.load',args='tables',ajax=True,user_signature=True)
    """
    return dict(form=crud())
